package com.zf.shuashua.ui.home.search;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.meis.base.mei.utils.SoftKeyboardUtils;
import com.zf.shuashua.R;
import com.zf.shuashua.base.BaseActivity;

import java.util.ArrayList;

public class SearchActivity extends BaseActivity {

    private String[] titles = {"视频", "用户"};
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private SearchVideoListFragment videoListFragment;
    private SearchUserListFragment userListFragment;
    private ViewPager viewPager;
    private SlidingTabLayout tabLayout;
    private MyPagerAdapter adapter;
    private EditText etSearch;
    private ImageView imgClear;

    @Override
    protected void initView() {
        viewPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);
        imgClear = findViewById(R.id.img_clear);
        etSearch = findViewById(R.id.et_search);
        setUpSearch();
        findViewById(R.id.tv_cancel).setOnClickListener(v -> {
            SoftKeyboardUtils.hideKeyboard(SearchActivity.this, v);
            finish();
        });
        imgClear.setOnClickListener(v -> etSearch.setText(""));

        adapter = new MyPagerAdapter(getSupportFragmentManager());
        videoListFragment = new SearchVideoListFragment();
        userListFragment = new SearchUserListFragment();
        fragments.add(videoListFragment);
        fragments.add(userListFragment);
        viewPager.setAdapter(adapter);
        tabLayout.setViewPager(viewPager, titles);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    private void setUpSearch() {
        etSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String key = etSearch.getText().toString();
                if (TextUtils.isEmpty(key)) {
                    return true;
                }
                SoftKeyboardUtils.hideKeyboard(SearchActivity.this, v);
                videoListFragment.reloadData(key);
                userListFragment.reloadData(key);
                return true;
            }
            return false;
        });
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String key = etSearch.getText().toString();
                if (TextUtils.isEmpty(key)) {
                    imgClear.setVisibility(View.GONE);
                    return;
                }
                imgClear.setVisibility(View.VISIBLE);
                videoListFragment.reloadData(key);
                userListFragment.reloadData(key);

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        String key = etSearch.getText().toString();
        if (!TextUtils.isEmpty(key)) {
            videoListFragment.reloadData(key);
            userListFragment.reloadData(key);
        }
    }

    @Override
    public void onBackPressedSupport() {
        SoftKeyboardUtils.hideKeyboard(SearchActivity.this, etSearch);
        super.onBackPressedSupport();
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }
    }

}
